#!/usr/bin/env python
""" Process .travis.yml and make sh file for testing
"""
from __future__ import print_function

import sys
from os.path import dirname, splitext
import warnings

import argparse # Python 2.6, really?

# pip install pyyaml
import yaml

sys.path.append(dirname(__file__))

from travisparse import get_envs, TravisError, get_yaml_entry

def main():
    parser = argparse.ArgumentParser(
        description='Extract sh script from .travis.yml file')
    parser.add_argument('in_yml', default='.travis.yml', nargs='?',
                        help = 'input travis YAML file '
                        '(default is ".travis.yml")')
    parser.add_argument('--no-py-env', action='store_true',
                        help = 'omit Python virtualenv building'
                        ' and substitute env vars that usually result')
    parser.add_argument('--out-sh',
                        help = 'output sh file name '
                        '(default is input name with .sh extension) '
                        '("-" means stdout)')
    parser.add_argument('-L', '--local', action='store_true',
                        help = 'shortcut for "--no-py-env --out-sh -"')
    args = parser.parse_args()
    if args.local:
        args.out_sh = '-'
        args.no_py_env = True
    with open(args.in_yml, 'rt') as fobj:
        travis_dict = yaml.load(fobj)
    out_sh = (splitext(args.in_yml)[0] + '.sh' if args.out_sh is None
              else args.out_sh)
    parts = ['# vars']
    try:
        parts.append(get_envs(travis_dict).strip())
    except TravisError:
        warnings.warn('Could not get travis vars from file')
    parts += ['# install'] + get_yaml_entry(travis_dict, 'install')
    parts += ['# test'] + get_yaml_entry(travis_dict, 'script')
    if args.no_py_env:
        parts = [part for part in parts
                 if not part.startswith('get_python_environment')]
        parts = ['# Variables usually set by `get_python_environment`',
                 'export PIP_CMD=pip',
                 'export PYTHON_EXE=python',
                 'export VIRTUALENV_CMD=virtualenv'] + parts
    content = '\n'.join(parts)
    if out_sh == '-':
        print(content)
        return
    with open(out_sh, 'wt') as fobj:
        fobj.write(content)
    print('Written ' + out_sh)


if __name__ == '__main__':
    main()
